﻿using System.Collections.Generic;
using System.Linq;
using UnityEngine.Assertions;

namespace Go
{
	public class GoString
	{
		public Player color;
		public HashSet<Point> stones;
		public HashSet<Point> liberties; // 气
		public GoString(Player color, HashSet<Point> stones, HashSet<Point> liberties)
		{
			this.color = color;
			this.stones = stones;
			this.liberties = liberties;
		}
		public void remove_liberty(Point point)
		{
			liberties.Remove(point);
		}
		public void add_liberty(Point point)
		{
			liberties.Add(point);
		}
		public GoString merge_with(GoString go_string)
		{
			Assert.IsTrue(color == go_string.color);

			var combined_stones = new HashSet<Point>(stones);
			combined_stones.UnionWith(go_string.stones);
			var combined_liberties = new HashSet<Point>(liberties);
			combined_liberties.UnionWith(go_string.liberties);
			combined_liberties.ExceptWith(combined_stones);
			return new GoString(color, combined_stones, combined_liberties);
		}
		public int num_liberties { get { return liberties.Count; } }
		public static bool operator ==(GoString lhs, GoString rhs)
		{
			return lhs.color == rhs.color && lhs.stones.SequenceEqual(rhs.stones) && lhs.liberties.SequenceEqual(rhs.liberties);
		}
		public static bool operator !=(GoString lhs, GoString rhs)
		{
			return lhs.color != rhs.color || !lhs.stones.SequenceEqual(rhs.stones) || !lhs.liberties.SequenceEqual(rhs.liberties);
		}
		public override bool Equals(object obj)
		{
			return this == (GoString)obj;
		}
		public override int GetHashCode()
		{
			return base.GetHashCode();
		}
	}
}
